﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;

namespace TMS.ServiceProvider.CapitalTransportationSolutions
{
    public class CTSBase
    {
        /// <summary>
        /// ID provided by CTS.
        /// </summary>
        public static int Uid { get; set; }

        public static Stream FetchStreamFromUrl(string strUrl)
        {
            Stream responseStream = null;

            HttpWebRequest request = WebRequest.Create(strUrl) as HttpWebRequest;
            if (request != null)
            {
                WebResponse response = request.GetResponse();
                responseStream = response.GetResponseStream();
            }

            return responseStream;
        }

        public static XmlReader FetchXmlReaderFromUrl(string strUrl, XmlReaderSettings settings)
        {
            XmlReader xmlReader = null;

            Stream responseStream = FetchStreamFromUrl(strUrl);

            if (responseStream != null)
            {
                xmlReader = (settings != null) ? XmlReader.Create(responseStream, settings) : XmlReader.Create(responseStream);
            }

            return xmlReader;
        }

        public static Stream GetStreamFromFile(string strFilePathIncludingName)
        {
            Stream stream = null;

            if(File.Exists(strFilePathIncludingName))
            {
                stream = File.Open(strFilePathIncludingName, FileMode.Open);
                stream.Close();
            }

            
            return stream;
        }

        public static XmlReader GetXmlReaderFromFile(string strFilePathIncludingName, XmlReaderSettings settings)
        {
            XmlReader xmlReader = null;

            Stream responseStream = GetStreamFromFile(strFilePathIncludingName);

            if (responseStream != null)
            {
                xmlReader = (settings != null) ? XmlReader.Create(responseStream, settings) : XmlReader.Create(responseStream);
            }

            return xmlReader;
        }
    }
}
